<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '175px',
    }"
  >
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model="formSearch.param.orderName"
          maxlength="20"
          placeholder="请输入工单名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formSearch.param.title"
          maxlength="50"
          placeholder="请输入工单标题"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-cascader
          style="width: 100%"
          v-model="formSearch.param.typeId"
          :options="typeData"
          @change="handleChange"
          :props="{
            value: 'id',
            label: 'typeName',
            children: 'childList',
            expandTrigger: 'hover',
            emitPath: false,
            checkStrictly: true,
          }"
          :checkStrictly="true"
          :show-all-levels="false"
          placeholder="请选择工单类型"
          ref="typeCascader"
          clearable
          popper-class="theder"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formSearch.param.date"
          @change="dateChange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始创建时间"
          end-placeholder="结束创建时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-other-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="orderName" label="工单名称"></el-table-column>
        <el-table-column prop="title" label="工单标题">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.title"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.title }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="工单类型"></el-table-column>
        <el-table-column prop="expireDataObj" label="处理时限">
          <template slot-scope="scope">
            <span v-if="scope.row.expireDataObj.type == 0"
              >{{ scope.row.expireDataObj.expire }}天</span
            >
            <span v-if="scope.row.expireDataObj.type == 1"
              >{{ scope.row.expireDataObj.expire }}小时</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="submitUserName" label="联系人"></el-table-column>
        <el-table-column
          prop="submitUserPhone"
          label="联系电话"
        ></el-table-column>
        <el-table-column prop="nodeName" label="处理人"></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="150px"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  command="approval"
                  v-if="$checkPermission(113)"
                  >审批</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer
      :title="title"
      :wrapperClosable="false"
      :size="666"
      :visible.sync="visible"
    >
      <basic-item :row="row" />
      <div style="text-align: right">
        <span class="wrap-btn wrap-success" @click="approval(1)">通过</span>
        <span class="wrap-btn wrap-danger" @click="approval(2)">驳回</span>
      </div>
    </el-drawer>
    <!-- 审批信息 -->
    <el-dialog title="审批信息" :visible.sync="isLook" width="888px">
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="审批意见">
          <el-input
            type="textarea"
            v-model="form.auditNote"
            maxlength="100"
            placeholder="请填写审批意见"
            rows="2"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件">
          <web-upload @upload="upload" type="file" />
          <el-table :data="form.auditFile" v-if="form.auditFile.length > 0">
            <el-table-column prop="name" label="名称">
              <template slot-scope="scope">
                <el-tooltip
                  popper-class="ellipsis-tip"
                  :content="scope.row.name"
                  placement="top-start"
                >
                  <span class="ellipsis">{{ scope.row.name }}</span>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column prop="size" label="大小"></el-table-column>
            <el-table-column
              prop="content"
              label="操作"
              width="100px"
              align="center"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="downloadFile(scope.row)"
                  >下载</el-link
                >
                <el-link
                  type="danger"
                  :underline="false"
                  style="margin-left: 10px"
                  @click="remove(scope.row)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="isLook = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import basicItem from './../components/basic'
export default {
  components: { basicItem },
  data() {
    return {
      typeData: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: { status: 0 },
      },
      total: null,
      tableData: [],
      visible: false,
      title: '审批详情',
      row: {},
      form: { auditFile: [] },
      isLook: false,
    }
  },
  created() {
    this.typeList()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post('/workorder/myToDoApprove/workOrderLnitiateSave', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'approval':
          this.$axios
            .get(
              '/workorder/myToDoApprove/findProcessWorkOrder?id=' +
                row.id +
                '&taskId=' +
                row.taskId
            )
            .then((data) => {
              this.row = {}
              if (data.code == 200) {
                this.row = data.data
                this.visible = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
      }
    },
    approval(type) {
      //审批
      this.form = { auditFile: [] }
      this.form.result = type
      this.isLook = true
    },
    save() {
      //保存
      if (!this.form.auditNote && this.form.result != 1) {
        this.$message.error('请填写审批意见')
        return false
      }
      this.form.id = this.row.id
      this.form.linkId = this.row.linkId
      this.$axios
        .post('/workorder/myToDoApprove/auditProcessWorkOrder', this.form)
        .then((data) => {
          if (data.code == 200) {
            this.visible = false
            this.isLook = false
            this.itemList()
            this.$message.success('操作成功！')
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    upload(data) {
      //上传
      this.form.auditFile.push(data)
    },
    dateChange(val) {
      if (val) {
        this.formSearch.param.startTime = val[0]
        this.formSearch.param.endTime = val[1]
      } else {
        this.formSearch.param.startTime = ''
        this.formSearch.param.endTime = ''
      }
    },
    typeList() {
      // 获取工单类型
      this.$axios
        .post('/workorder/configWorkOrderTypeManage/workTypeList')
        .then((data) => {
          this.typeData = []
          if (data.code == 200) {
            this.typeData = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    handleChange() {
      this.$refs.typeCascader.dropDownVisible = false //监听值发生变化就关闭它
    },
    downloadFile(item) {
      //下载附件
      const a = document.createElement('a')
      a.download = item.name
      a.href = item.fileDomain + item.url
      a.click()
    },
    remove(item) {
      //删除
      var index = this.form.auditFile.indexOf(item)
      if (index !== -1) {
        this.form.auditFile.splice(index, 1)
      }
    },
  },
}
</script>
